package com.sen.common.widget.form;

import org.apache.commons.lang.StringUtils;

import com.sen.common.widget.Attributes;

/**
 * @author sen
 */
public abstract class CheckableInput extends Input {

	private String checked;
	private Label component;

	public CheckableInput(String type) {
		this(type, null);
	}

	public CheckableInput(String type, String label) {
		this(type, null, label);
	}

	public CheckableInput(String type, String id, String label) {
		this(type, id, null, label);
	}

	public CheckableInput(String type, String id, String name, String label) {
		super(type);
		this.id(id).name(name);
		label(label, id);
	}

	public CheckableInput checked() {
		this.checked = "checked";
		return this;
	}

	public CheckableInput unchecked() {
		this.checked = null;
		return this;
	}

	public final CheckableInput label(String label) {
		return label(label, null);
	}

	public final CheckableInput label(String label, String labelFor) {
		this.id(labelFor);
		component = StringUtils.isEmpty(label) ? null : new Label(label)
				.forId(labelFor);
		return this;
	}
	
	@Override
	protected String specialAttributes() {
		return super.specialAttributes()
				+ Attributes.attrIfNonblank("checked", checked);
	}

	@Override
	public String toHtml() {
		return super.toHtml() + (null == component ? "" : component.toHtml());
	}

}
